package com.lancoo.cvas.util;

import cn.hutool.core.collection.CollUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.Consumer;

/**
 * @ClassName BatchInsert
 * @Description TODO
 * @Author quin
 * @Date 2022/12/8 10:09
 */
public class BatchInsert {

    public static int maxInsertItemNumPerTime = 1000;

    public static  <T> void batchSplitInsert(List<T> list, Consumer<List<T>> insertFunc) {
        List<List<T>> all = new ArrayList<>();
        if (list.size() > maxInsertItemNumPerTime ) {
            int i = 0;
            while (i < list.size()) {
                List<T> subList;
                if (i + maxInsertItemNumPerTime  > list.size()){
                    subList = list.subList(i, list.size());
                }else {
                    subList = list.subList(i, i + maxInsertItemNumPerTime );
                }
                i = i + maxInsertItemNumPerTime ;
                all.add(subList);
            }
            all.forEach(insertFunc);
//            all.parallelStream().forEach(insertFunc);
        } else {
            if(CollUtil.isNotEmpty(list)){
                //如果list不为空，且list.size<1000 直接对整个列表进行插入
                insertFunc.accept(list);
            }

        }
    }

    public static void main(String[] args) {

        HashMap<Object, Object> roomTypeMap = new HashMap<>();

        roomTypeMap.put(11, "云网络教室");
        roomTypeMap.put(21, "A1智慧教室");
        roomTypeMap.put(22, "传统多媒体教室");
        roomTypeMap.put(23, "云演播室");
        roomTypeMap.put(24, "多媒体智慧教室");
        roomTypeMap.put(31, "普通电脑室");
        roomTypeMap.put(32, "2018c语音室");
        roomTypeMap.put(45, "智慧操场/体育场所");
        roomTypeMap.put(46, "实验室");
        System.out.println(roomTypeMap);
    }
//    public  void test() {
//        List<Schedule> list = new ArrayList<>();
//        Consumer<List<Schedule>> consumer = o -> scheduleMapper.batchInsert(o);
//        batchSplitInsert(list,consumer);
//    }
}
